//
//  TTSetPasswordViewController.m
//  TaoTuo
//
//  Created by admin on 2019/12/19.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTSetPasswordViewController.h"

@interface TTSetPasswordViewController ()

@property (nonatomic, strong) UITextField *oldTF;
@property (nonatomic, strong) UITextField *newaTF;
@property (nonatomic, strong) UITextField *newbTF;

@end

@implementation TTSetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置密码";
    
    [self setupSubviews];
    
}

- (void)confirmModifyAction
{
    if (_oldTF.text.length < 6) {
        [self.view makeToast:@"旧密码长度有误"];
        return;
    }
    if (_newaTF.text.length < 6) {
        [self.view makeToast:@"输入新密码长度有误"];
        return;
    }
    if (_newbTF.text.length < 6) {
        [self.view makeToast:@"输入确认密码长度有误"];
        return;
    }
    
    if (![_newaTF.text isEqualToString:_newbTF.text]) {
        [self.view makeToast:@"新密码输入不一致"];
        return;
    }
    
    NSDictionary *dic = @{
        @"param.accountId" : [TTLoginSingle sharedSingle].loginModel.AccountId,
        @"param.newPassword" : _newaTF.text,
        @"param.oldPassword" : _oldTF.text
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:modifyPassword_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        [[UIApplication sharedApplication].keyWindow makeToast:@"密码修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
    
}

- (void)setupSubviews
{
    _oldTF = [self createTFWithFrame:CGRectMake(15, 0, self.view.width - 30, 40) placeholder:@"请输入旧密码"];
    [self.view addSubview:_oldTF];
    
    _newaTF = [self createTFWithFrame:CGRectMake(_oldTF.left, _oldTF.bottom, _oldTF.width, _oldTF.height) placeholder:@"请输入新密码"];
    [self.view addSubview:_newaTF];
    
    _newbTF = [self createTFWithFrame:CGRectMake(_oldTF.left, _newaTF.bottom, _oldTF.width, _oldTF.height) placeholder:@"请再次输入新密码"];
    [self.view addSubview:_newbTF];
    
    UIButton *confirmBtn = [UIButton createButtonWithTitle:@"确认修改" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:18] backgroundColor:kMainColor target:self action:@selector(confirmModifyAction)];
    confirmBtn.layer.cornerRadius = 22.5;
    confirmBtn.frame = CGRectMake(15, _newbTF.bottom + 30, self.view.width - 30, 45);
    [self.view addSubview:confirmBtn];
    
}

- (UITextField *)createTFWithFrame:(CGRect)frame placeholder:(NSString *)placeholder
{
    UITextField *tf = [[UITextField alloc] initWithFrame:frame];
    tf.textColor = UIColorHex(333333);
    [tf setPlaceholder:placeholder lightColor:nil darkColor:nil];
    tf.font = [UIFont systemFontOfSize:15];
    tf.secureTextEntry = YES;
    return tf;
}


@end
